package com.zhj.algorithm.search.str;

public class SundaySearch implements StrSearch{

    @Override
    public int search(String A, String B) {
        if (A == null || A.length() == 0 || B == null || B.length() == 0) {
            return -1;
        }
        int start = 0;
        while (start <= A.length() - B.length()) {
            if (A.charAt(start) == B.charAt(0)) {
                Boolean flag = true;
                for (int i = 1; i < B.length(); i++) {
                    if (A.charAt(start + i) != B.charAt(i)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    return start + 1;
                }
                start += 1;
            } else {
                start += B.length();
                if (start >= A.length() - B.length()) {
                    return -1;
                }
                Boolean exist = false;
                for (int i = 0; i < B.length(); i++) {
                    if (A.charAt(start) == B.charAt(i)) {
                        exist = true;
                        start = start - i;
                        break;
                    }
                }
                if (!exist) {
                    start += 1;
                }
            }
        }
        return -1;
    }
}
